//
//  LayoutJumpViewController.m
//  Item A House
//
//  Created by dlios on 15/5/26.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "LayoutJumpViewController.h"
#import <UIImageView+WebCache.h>
@interface LayoutJumpViewController ()<UIScrollViewDelegate>

@end

@implementation LayoutJumpViewController
- (void)dealloc
{
    [_yuegong release];
    [_price release];
    [_alias release];
    [_name release];
    [_area release];
    [_orient release];
    [_scroll release];
    [_layoutJump release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.layoutJump = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1]];
    [self createScroll];
    [self.navigationItem setHidesBackButton:YES];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    btn.frame = CGRectMake(15, 5, 38, 38);
    
    [btn setBackgroundImage:[UIImage imageNamed:@"返回.png"] forState:UIControlStateNormal];
    
    [btn addTarget: self action: @selector(goBackAction) forControlEvents: UIControlEventTouchUpInside];
    
    UIBarButtonItem*back=[[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem=back;
}
-(void)goBackAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)createScroll
{
    
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, [UIScreen mainScreen].bounds.size.width * 0.8)];
    self.scroll.delegate = self;
    self.scroll.pagingEnabled = YES;
    self.scroll.bounces = NO;
    [self.view addSubview:self.scroll];
    for (int i = 0; i < [self.layoutJump count]; i++) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(self.scroll.frame.size.width * i, 0, self.scroll.frame.size.width, self.scroll.frame.size.height)];
        
        [view setBackgroundColor:[UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:0.5]];
        [self.scroll addSubview:view];
        
        
        NSDictionary *bigDic = [self.layoutJump objectAtIndex:i];
        NSMutableArray *row = [bigDic objectForKey:@"rows"];
        NSDictionary *smallDic = [row objectAtIndex:0];
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, self.scroll.frame.size.width - 40, view.frame.size.width - 100)];
        image.userInteractionEnabled = YES;
        [image sd_setImageWithURL:[smallDic objectForKey:@"default_image"] placeholderImage:[UIImage imageNamed:@"10.jpg"]];
        [view addSubview:image];
        [image release];
        [view release];
        
    }
    [self.scroll setContentSize:CGSizeMake([self.layoutJump count] * self.scroll.frame.size.width, 0)];
    [self.scroll release];
    
    NSDictionary *bigDic = [self.layoutJump objectAtIndex:0];
    NSMutableArray *row = [bigDic objectForKey:@"rows"];
    NSDictionary *smallDic = [row objectAtIndex:0];
    
    self.alias = [[UILabel alloc] initWithFrame:CGRectMake(10, [UIScreen mainScreen].bounds.size.width * 0.8, [UIScreen mainScreen].bounds.size.width, 20)];
    self.alias.text = [smallDic objectForKey:@"alias"];
    [self.alias setFont:[UIFont fontWithName:@"Helvetica-Bold" size:16]];
    [self.view addSubview:self.alias];
    [self.alias release];
    
    self.name = [[UILabel alloc] initWithFrame:CGRectMake(10, [UIScreen mainScreen].bounds.size.width * 0.8 + 20, [UIScreen mainScreen].bounds.size.width, 20)];
    self.name.text = [NSString stringWithFormat:@"户型:%@", [smallDic objectForKey:@"name"]];
    self.name.textColor = [UIColor colorWithWhite:0.3 alpha:0.8];
    self.name.font = [UIFont systemFontOfSize:16];;
    [self.view addSubview:self.name];
    [self.name release];
    
    self.area = [[UILabel alloc] initWithFrame:CGRectMake(10, [UIScreen mainScreen].bounds.size.width * 0.8 + 40, [UIScreen mainScreen].bounds.size.width, 20)];
    self.area.text = [NSString stringWithFormat:@"面积:%@平", [smallDic objectForKey:@"area"]];
    self.area.textColor = [UIColor colorWithWhite:0.3 alpha:0.8];
    self.area.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:self.area];
    [self.area release];
    
    self.orient = [[UILabel alloc] initWithFrame:CGRectMake(10, [UIScreen mainScreen].bounds.size.width * 0.8 + 60, [UIScreen mainScreen].bounds.size.width, 20)];
    self.orient.text = [NSString stringWithFormat:@"朝向:%@", [smallDic objectForKey:@"orient"]];
    self.orient.font = [UIFont systemFontOfSize:16];
    self.orient.textColor = [UIColor colorWithWhite:0.3 alpha:0.8];
    [self.view addSubview:self.orient];
    [self.orient release];
    
    self.price = [[UILabel alloc] initWithFrame:CGRectMake(10, [UIScreen mainScreen].bounds.size.width * 0.8 + 80, [UIScreen mainScreen].bounds.size.width, 20)];
    self.price.text = [NSString stringWithFormat:@"价格:%@/平", [smallDic objectForKey:@"price"]];
    self.price.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:self.price];
    [self.price release];
    
    self.yuegong = [[UILabel alloc] initWithFrame:CGRectMake(10, [UIScreen mainScreen].bounds.size.width * 0.8 + 100, [UIScreen mainScreen].bounds.size.width, 20)];
    self.yuegong.text = [NSString stringWithFormat:@"月供 : %@元", [smallDic objectForKey:@"yuegong"]];
    self.yuegong.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:self.yuegong];
    [self.yuegong release];
    

}
//滚动停止时执行
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger page = scrollView.contentOffset.x / self.scroll.frame.size.width;
    NSDictionary *bigDic = [self.layoutJump objectAtIndex:page];
    NSMutableArray *row = [bigDic objectForKey:@"rows"];
    NSDictionary *smallDic = [row objectAtIndex:0];
    self.alias.text = [smallDic objectForKey:@"alias"];
    self.name.text = [NSString stringWithFormat:@"户型:%@", [smallDic objectForKey:@"name"]];
    self.area.text = [NSString stringWithFormat:@"面积:%@平", [smallDic objectForKey:@"area"]];
    self.orient.text = [NSString stringWithFormat:@"朝向:%@", [smallDic objectForKey:@"orient"]];
    self.price.text = [NSString stringWithFormat:@"价格:%@/平", [smallDic objectForKey:@"price"]];
    self.yuegong.text = [NSString stringWithFormat:@"月供 : %@元", [smallDic objectForKey:@"yuegong"]];
}

@end
